package com.wangshuqi.jdbc.dao;

import com.wangshuqi.jdbc.bean.Customer;

import java.sql.Connection;
import java.util.List;

public class CustomerDAOImpl extends BaseDAO<Customer> implements CustomerDAO {

    @Override
    public int insertCustomer(Connection connection, Customer customer) {
        String sql = "insert into customers(name,email,birth) values(?,?,?)";
        return saveOrUpdateOrDelete(connection, sql, customer.getName(), customer.getEmail(), customer.getBirth());
    }

    @Override
    public int updateById(Connection connection, Customer customer) {
        String sql = "update customers set name = ?,email=?,birth=? where id = ?";
        return saveOrUpdateOrDelete(connection, sql,
                customer.getName(), customer.getEmail(), customer.getBirth(), customer.getId());
    }

    @Override
    public int deleteById(Connection connection, Integer id) {
        String sql = "delete from customers where id = ?";
        return saveOrUpdateOrDelete(connection, sql, id);
    }

    @Override
    public Customer selectById(Connection connection, Integer id) {
        String sql = "select id,name,email,birth from customers where id = ?";
        return selectBean(connection, sql, id);
    }

    @Override
    public List<Customer> selectAll(Connection connection) {
        String sql = "select id,name,email,birth from customers";
        return selectBeanList(connection, sql);
    }

    @Override
    public Long count(Connection connection) {
        String sql = "select count(1) from customers";
        return selectValue(connection, sql);
    }
}
